<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\service\GoodsDetail;
use Biz\service\GoodsInfo;

$noWikiGoods = mGoodsBasicInfo::getInfos(['wiki_id' => 0]);

if ($noWikiGoods) {
    $goodsIds = array_column($noWikiGoods, 'goods_id');
    $result=GoodsDetail::GoodsInfoList($goodsIds);
    var_dump($result);
//$lastCategory_id = $result['category_id'];
//if ($result['four_category_id']) {
//    $lastCategory_id = $result['four_category_id'];
//} else if ($result['third_category_id']) {
//    $lastCategory_id = $result['third_category_id'];
//} else if ($result['second_category_id']) {
//    $lastCategory_id = $result['second_category_id'];
//}
//

//$wikiData = [
//    'wiki_id' => $post['wiki_id'],
//    'title' => $post['title'],
//    'goods_id' => $result['goods_id'],
//    'brand_id' => $post['brand_id'],
//    'category_id' => $result['category_id'],
//    'last_category_id' => $lastCategory_id,
//    'spec_img' => $result['spec_img'],
//    'images_content' => json_encode($post['images_content']),
//    'style_content' => $post['style_content'],
//    'min_price' => $result['max_min_price']['min_price'],
//    'max_price' => $result['max_min_price']['max_price'],
//    'material_content' => $post['material_content'],
//    'extinfo' => array_map(function ($v) {
//        return $v['extinfo'];
//    }, $post['sku_attr'])
//];
//
//GoodsInfo::createOrSyncWiki($wikiData);
    $goodsList = GoodsDetail::GoodsInfoList($goodsIds);
    foreach ($goodsList as $goods) {
        $lastCategory_id = $goods['category_id'];
        if ($goods['four_category_id']) {
            $lastCategory_id = $goods['four_category_id'];
        } else if ($goods['third_category_id']) {
            $lastCategory_id = $goods['third_category_id'];
        } else if ($goods['second_category_id']) {
            $lastCategory_id = $goods['second_category_id'];
        }

        $wikiData = [
            'wiki_id' => 0,
            'title' => $goods['title'],
            'goods_id' => $goods['goods_id'],
            'brand_id' => $goods['brand_id'],
            'category_id' => $goods['category_id'],
            'last_category_id' => $lastCategory_id,
            'spec_img' => $goods['spec_img'],
            'images_content' => json_encode($goods['images_content_hash']),
            'style_content' => $goods['style_content'],
            'min_price' => $goods['min_price'],
            'max_price' => $goods['max_price'],
            'material_content' => $goods['spec_params'],
            'extinfo' => $goods['extinfo']
        ];
        GoodsInfo::createOrSyncWiki($wikiData);
    }
}